﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.DKChung;

namespace QuanLyKho.BanHang
{
    public partial class DanhSachSPCuaCuaHang : Form
    {
        public static string CuaHang = "0";
        public static string IdSanPham = "0";
        public static bool Loai = false;
        public string MaSanPham = "";

        public DanhSachSPCuaCuaHang()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
            SetPropertiesColumns();
        }

        public void Loaddata()
        {
            if (Loai)
            {
                var data = DataAll.SelectAll("SanPhamTrongCuaHang LEFT JOIN [dbo].[SanPhamHoanThien] ON [SanPhamTrongCuaHang].IDSpHoanThien = [SanPhamHoanThien].ID"
                , "[SanPhamTrongCuaHang].SoLuong AS'SoLuongChia',SanPhamTrongCuaHang.IDSpHoanThien AS'ID1',SanPhamTrongCuaHang.ID AS'IDTCH',*",
                " WHERE [SanPhamTrongCuaHang].TenCuaHang=N'" + CuaHang + "'");
                dtNgayChia.Value = Convert.ToDateTime(data.Rows[0]["NgayChia"]);
                txtCuaHang.Text = data.Rows[0]["TenCuaHang"].ToString();
                txtNguoiChia.Text = data.Rows[0]["NguoiChia"].ToString();
                dtNgayChungTu.Value = Convert.ToDateTime(data.Rows[0]["NgayChungTu"]);
                dtNgayHachToan.Value = Convert.ToDateTime(data.Rows[0]["NgayHoachToan"]);
                txtChungtu.Text = data.Rows[0]["SoChungTu"].ToString();
                gridSanPham.DataSource = null;
                gridSanPham.DataSource = data;
            }
            else
            {
                var data = DataAll.SelectAll("HangTrongKho LEFT JOIN [dbo].[SanPhamHoanThien] ON [HangTrongKho].IDSpHoanThien = [SanPhamHoanThien].ID"
                , "[HangTrongKho].SoLuong AS'SoLuongChia',HangTrongKho.IDSpHoanThien AS'ID1',HangTrongKho.ID AS'IDTCH',*",
                " WHERE [HangTrongKho].TenKho=N'" + CuaHang + "'");
                dtNgayChia.Value = Convert.ToDateTime(data.Rows[0]["NgayChia"]);
                txtNguoiChia.Text = data.Rows[0]["NguoiChia"].ToString();
                dtNgayChungTu.Value = Convert.ToDateTime(data.Rows[0]["NgayChungTu"]);
                dtNgayHachToan.Value = Convert.ToDateTime(data.Rows[0]["NgayHoachToan"]);
                txtChungtu.Text = data.Rows[0]["SoChungTu"].ToString();
                txtKhoCat.Text = data.Rows[0]["TenKho"].ToString();
                gridSanPham.DataSource = null;
                gridSanPham.DataSource = data;
            }
        }

        public void SetPropertiesGrid()
        {
            gvSanPham.BestFitColumns();
            gvSanPham.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvSanPham.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvSanPham.OptionsSelection.MultiSelect = true;
            gvSanPham.OptionsView.ShowGroupPanel = false;
            gvSanPham.OptionsView.ColumnAutoWidth = false;
            gvSanPham.OptionsView.ShowFooter = true;
            gvSanPham.OptionsFind.AlwaysVisible = true;
            //gvSanPham.OptionsBehavior.Editable = false;
            gvSanPham.Columns["ID1"].Visible = false;
            gvSanPham.Columns["IDTCH"].Visible = false;
            for (int i = 2; i < gvSanPham.Columns.Count; i++)
            {
                gvSanPham.Columns[i].OptionsColumn.AllowEdit = false;
            }
        }

        public void SetPropertiesColumns()
        {
            gcSize.OptionsColumn.AllowEdit = false;

            gcSoLuong.DisplayFormat.FormatType = FormatType.Numeric;
            gcSoLuong.DisplayFormat.FormatString = "{0:0,0}";
        }

        public void LoadImage(string id)
        {
            var MaAnh = DataAll.SelectAll("SanPhamHoanThien", "*", " WHERE ID=" + id + "");
            var data = DataAll.SelectAll("DkChung_HinhAnh", "*", " WHERE MaAnh=N'" + MaAnh.Rows[0]["MaHang"] + "'");
            if (data.Rows.Count > 0)
            {
                var content = (byte[])data.Rows[0]["Anh"];
                if (content != null)
                {
                    Image newImage;
                    using (var ms = new MemoryStream(content, 0, content.Length))
                    {
                        ms.Write(content, 0, content.Length);
                        newImage = Image.FromStream(ms, true);
                    }
                    pbAnh.Image = newImage;
                }
                else
                {
                    pbAnh.Image = null;
                }
            }
            else
            {
                MessageBox.Show("Không tìm thấy mã ảnh", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void pbAnh_Click(object sender, EventArgs e)
        {
            var MaAnh = DataAll.SelectAll("SanPhamHoanThien", "MaHang", " WHERE ID=" + txtIdSP.Text);
            XemAnh.MaID = MaAnh.Rows[0]["MaHang"].ToString();
            var anh = new XemAnh();
            anh.ShowDialog();
            XemAnh.MaID = "0";
        }

        private void bntXoa_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (gvSanPham.RowCount > 1)
            {
                var soluongcu = DataAll.SelectAll("SanPhamHoanThien", "SoLuong", " WHERE ID =" + gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "ID1"));
                int soluongmoi = Convert.ToInt32(soluongcu.Rows[0]["SoLuong"].ToString()) +
                                 Convert.ToInt32(gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "SoLuongChia"));
                string message = DataAll.Update("SanPhamHoanThien", " SoLuong=" + soluongmoi +
                    " WHERE ID =" + gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "ID1"));
                if (message.Equals("success"))
                {
                    message = DataAll.Delete("SanPhamTrongCuaHang", " IDSpHoanThien=" + gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "ID1"));
                }
                MessageBox.Show(message.Equals("success") ? "Xóa thành công" : message, "Message",
                                                MessageBoxButtons.OK,
                                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                Loaddata();
            }
        }

        private void txtText_Click(object sender, EventArgs e)
        {

        }

        private void gvSanPham_RowClick(object sender, RowClickEventArgs e)
        {
            if (gvSanPham.RowCount > 0 && e.RowHandle < gvSanPham.RowCount)
            {
                var data = DataAll.SelectAll("SanPhamTrongCuaHang LEFT JOIN [dbo].[SanPhamHoanThien] ON [SanPhamTrongCuaHang].IDSpHoanThien = [SanPhamHoanThien].ID"
                    , "SanPhamHoanThien.Size,SanPhamTrongCuaHang.SoLuong,SanPhamHoanThien.LoaiHang,SanPhamHoanThien.LanSanXuat,SanPhamHoanThien.LenhSanXuat,SanPhamHoanThien.ID,SanPhamHoanThien.MaHang,SanPhamHoanThien.NgayLL"
                    , " WHERE SanPhamTrongCuaHang.ID = " + gvSanPham.GetRowCellValue(e.RowHandle, "IDTCH"));
                gvSanPham.SetRowCellValue(e.RowHandle, "Size", data.Rows[0]["Size"].ToString());
                txtMaSanPham.Text = gvSanPham.GetRowCellDisplayText(e.RowHandle, "MaHang");
                txtIdSP.Text = data.Rows[0]["ID"].ToString();
                txtSoLuong.Text = data.Rows[0]["SoLuong"].ToString();
                txtLoaiHang.Text = data.Rows[0]["LoaiHang"].ToString();
                txtLanTai.Text = data.Rows[0]["LanSanXuat"].ToString();
                txtSoLenh.Text = data.Rows[0]["LenhSanXuat"].ToString();
                dtNgayLL.Value = Convert.ToDateTime(data.Rows[0]["NgayLL"].ToString());
                LoadImage(data.Rows[0]["ID"].ToString());
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
